<?php
include("include/config.inc.php");
$incommingArr = array();
$processArr = array();

$selectincomingProcess = "SELECT planning.incomingId,processId1,processId2,processId3,pro1.processName AS proName1,
                                 pro2.processName AS proName2,pro3.processName AS proName3,
                                 temp1,soaking1,furnace1.furnaceName AS fName1,
                                 temp2,soaking2,furnace2.furnaceName AS fName2,
                                 temp3,soaking3,furnace3.furnaceName AS fName3,okNotOk1,okNotOk2,okNotOk3
                            FROM planning
                            JOIN process AS pro1 ON planning.processId1 = pro1.processId
                            LEFT JOIN process AS pro2 ON planning.processId2 = pro2.processId
                            LEFT JOIN process AS pro3 ON planning.processId3 = pro3.processId
                            LEFT JOIN furnace AS furnace1 ON planning.fId1 = furnace1.fId
                            LEFT JOIN furnace AS furnace2 ON planning.fId2 = furnace2.fId
                            LEFT JOIN furnace AS furnace3 ON planning.fId3 = furnace3.fId
                           WHERE planningId = ".$_GET['planningId'];
$selectincomingProcessRes = mysql_query($selectincomingProcess);
if($processRow = mysql_fetch_array($selectincomingProcessRes))
{
  $incommingArr['logSheetPlanningId'] = $_GET['planningId'];
  $incommingArr['logSheetIncomingId'] = $_GET['incomingId'];
  
  $incommingArr['temp1']        = $processRow['temp1'];
  $incommingArr['soaking1']     = $processRow['soaking1'];
  $incommingArr['furnace1']     = $processRow['fName1'];
  
  $incommingArr['temp2']        = $processRow['temp2'];
  $incommingArr['soaking2']     = $processRow['soaking2'];
  $incommingArr['furnace2']     = $processRow['fName2'];
  
  $incommingArr['temp3']        = $processRow['temp3'];
  $incommingArr['soaking3']     = $processRow['soaking3'];
  $incommingArr['furnace3']     = $processRow['fName3'];
  
  $incommingArr['processName1'] = $processRow['proName1'];  
  $incommingArr['processName2'] = $processRow['proName2'];
  $incommingArr['processName3'] = $processRow['proName3'];
  
  $selectEmptySheet = "SELECT * FROM planning WHERE planningId = ".$_GET['planningId'];
  $selRes = mysql_query($selectEmptySheet);
  $j = 0;
  if($planRow = mysql_fetch_array($selRes))
  {
  	if($planRow['processId1'] > 0 && $planRow['temp1'] > 0 && $planRow['logSheet1'] == 0)
  	{
  		$j++;
  		$incommingArr['processId'][$j] = $planRow['processId1'];
  		$incommingArr['nameOfProcess'][$j] = $processRow['proName1'];
  		$incommingArr['processNo'][$j] = 1;
  	}
  	if($planRow['processId2'] > 0 && $planRow['temp2'] > 0 && $planRow['logSheet2'] == 0)
  	{
  		$j++;
  		$incommingArr['processId'][$j] = $planRow['processId2'];
  		$incommingArr['nameOfProcess'][$j] = $processRow['proName2'];
  		$incommingArr['processNo'][$j] = 2;
  	}
  	if($planRow['processId3'] > 0 && $planRow['temp3'] > 0 && $planRow['logSheet3'] == 0)
  	{
  		$j++;
  		$incommingArr['processId'][$j] = $planRow['processId3'];
  		$incommingArr['nameOfProcess'][$j] = $processRow['proName3'];
  		$incommingArr['processNo'][$j] = 3;
  	}
  	
  	/////////////////////////////////
  	for($processLoop = 1; $processLoop <= 3; $processLoop++)
  	{
  	  if(isset($planRow['logSheet'.$processLoop]) && $planRow['logSheet'.$processLoop] > 0)
  	  {
	      $selectincomingProcess = "SELECT logsheet.processId,logsheetNo,DATE_FORMAT(logsheetDate,'%d-%m-%Y') AS logsheetDate,
	                                       DATE_FORMAT(startTime,'%H-%i')AS startTime,startTemperature,DATE_FORMAT(soakingStartTime,'%H-%i')AS soakingStartTime,
	                                       DATE_FORMAT(endTime,'%H-%i')AS endTime,endTemperature,
                                         qMName,quenchingTime,tempBforQue,tempAfterQue,processName,logsheet.status
                                    FROM logsheet
                                    JOIN process ON process.processId = logsheet.processId
                                    JOIN quenchingmedia ON logsheet.qMId = quenchingmedia.qMId
                                   WHERE logsheetId = ".$planRow['logSheet'.$processLoop];
        $selectincomingProcessRes = mysql_query($selectincomingProcess);
        if($processRow = mysql_fetch_array($selectincomingProcessRes))
        {
          $processArr[$processLoop-1]['processNo']          = $processRow['processId'];
          $processArr[$processLoop-1]['processName']        = $processRow['processName'];
          $processArr[$processLoop-1]['logsheetNo']         = $processRow['logsheetNo'];
          $processArr[$processLoop-1]['logsheetDate']       = $processRow['logsheetDate'];
          $processArr[$processLoop-1]['startTime']          = $processRow['startTime'];
          $processArr[$processLoop-1]['startTemperature']   = $processRow['startTemperature'];
          $processArr[$processLoop-1]['soakingStartTime']   = $processRow['soakingStartTime'];
          $processArr[$processLoop-1]['endTime']            = $processRow['endTime'];
          $processArr[$processLoop-1]['endTemperature']     = $processRow['endTemperature'];
          $processArr[$processLoop-1]['quenchingMedia']     = $processRow['qMName'];
          $processArr[$processLoop-1]['quenchingTime']      = $processRow['quenchingTime'];
          $processArr[$processLoop-1]['tempBforQue']        = $processRow['tempBforQue'];
          $processArr[$processLoop-1]['tempAfterQue']       = $processRow['tempAfterQue'];
          $processArr[$processLoop-1]['status']             = $processRow['status'];
          $processArr[$processLoop-1]['incomingId']         = $_REQUEST['incomingId'];
        }
      }
    }
  	/////////////////////////////////
  }
}
//-----------------Quenching Media-----------------------------------
$q = 0;                                                                     
$select = "SELECT qMId,qMName                                    
             FROM quenchingmedia";                                                   
$selectResult = mysql_query($select);                                       
while($row = mysql_fetch_array($selectResult))                              
{                                                                           
  $qmArray['qMId'][$q]   = $row['qMId'];                    
  $qmArray['qMName'][$q] = $row['qMName'];                      
  $q++;                                                                     
}                                                                           

$smarty->assign("j",$j);
$smarty->assign("incommingArr",$incommingArr);
$smarty->assign("processArr",$processArr);
$smarty->assign("qmArray",$qmArray);
$smarty->display('getHide2.tpl');